package com.qf.android.sport.adapter.headline;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qf.android.sport.BR;
import com.qf.android.sport.databinding.LayoutHeadlineHeadlineBinding;
import com.qf.android.sport.entity.headline.Head;

import java.util.List;

/**
 * Created by wen10 on 2016/12/16.
 */

public class HeadlineHeadlineRecyclerAdapter extends RecyclerView.Adapter<HeadlineHeadlineRecyclerAdapter.ViewHolder>{
    private List<Head.DataBean.HeadsBean> listHeadsBeen;
    private int variableId;
    public static final int FIRST_STICKY_VIEW = 1;
    // RecyclerView 除了第一个item以外，要展示StickyLayout的.
    public static final int HAS_STICKY_VIEW = 2;
    // RecyclerView 的不展示StickyLayout的item.
    public static final int NONE_STICKY_VIEW = 3;

    public HeadlineHeadlineRecyclerAdapter(List<Head.DataBean.HeadsBean> listHeadsBeen) {
        this.listHeadsBeen = listHeadsBeen;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = new ViewHolder(LayoutHeadlineHeadlineBinding.inflate(LayoutInflater.from(parent.getContext()),parent,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

            holder.binding.setVariable(BR.headBean,listHeadsBeen.get(position));
            Head.DataBean.HeadsBean headsBean = (Head.DataBean.HeadsBean) listHeadsBeen.get(position);

            //与顶部悬浮布局（吸顶布局）有关
            if (position == 0){
                holder.binding.headlineHeadlineRecyclerItemCv.setVisibility(View.VISIBLE);
                holder.binding.headlineHeadlineRecyclerItemCv.setTag(NONE_STICKY_VIEW);
            }
            else {
                if(!TextUtils.equals(headsBean.getPublic_time(),listHeadsBeen.get(position -1).getPublic_time())){
                    holder.binding.headlineStickyTv.setVisibility(View.VISIBLE);
                    holder.binding.headlineStickyTv.setText(headsBean.getPublic_time());
                    holder.binding.headlineHeadlineRecyclerItemCv.setTag(HAS_STICKY_VIEW);
                }else {
                    holder.binding.headlineStickyTv.setVisibility(View.GONE);
                    holder.binding.headlineHeadlineRecyclerItemCv.setTag(NONE_STICKY_VIEW);


                }

            }
            holder.binding.headlineHeadlineRecyclerItemCv.setContentDescription(headsBean.getPublic_time());





    }

    public void addAll(List<Head.DataBean.HeadsBean> headsBeen ){
        int position = listHeadsBeen.size()-1;
        listHeadsBeen.addAll(headsBeen);
        notifyItemRangeInserted(position,position+headsBeen.size());
    }
    public void removeAll(){
        listHeadsBeen.clear();
        notifyItemRangeRemoved(0,listHeadsBeen.size()-1);
    }

    @Override
    public int getItemCount() {
        return listHeadsBeen.size();
    }
//
//    @Override
//    public int getItemViewType(int position) {
//        int viewType = 0;
//        Head.DataBean.HeadsBean headsBean = headsBeen.get(position);
//        if (headsBean.getContest_content() == null){
//
//            viewType = headsBean.getSubtitle() == null?R.layout.layout_headline_normal:R.layout.layout_headline_subtitle;
//
//
//
//        }else {
//            viewType = R.layout.layout_headline_headline;
//        }
//
//        return viewType;
//    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private LayoutHeadlineHeadlineBinding binding;
        public ViewHolder(LayoutHeadlineHeadlineBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }


    }
}
